using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Threading;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;
using Microsoft.Office.Project.EPMAuditing.ReceiverService.Monitor.Properties;

namespace Microsoft.Office.Project.EPMAuditing.ReceiverService.Monitor
{
    class NotifyIconUpdate
    {
        private NotifyIcon _notifyIcon = null;
        ManualResetEvent _pause = null;
        string _serviceName = string.Empty;

        public NotifyIconUpdate(NotifyIcon ni, string ServiceName, ManualResetEvent Pause)
        {
            _notifyIcon = ni;
            _pause = Pause;
            _serviceName = ServiceName;
        }

        public static ServiceController ServiceStartup(string ServiceName)
        {
            try
            {
                ServiceController serviceController = new ServiceController(ServiceName);
                return serviceController;
            }
            catch (ArgumentException)
            {
                return null;
            }
        }

        public static ServiceController WaitForServiceStartup(string ServiceName, int Timeout)
        {
            DateTime timeoutTime = DateTime.Now.AddSeconds(Timeout);

            do
            {
                if (DateTime.Now > timeoutTime)
                    return null;

                ServiceController serviceController = ServiceStartup(ServiceName);
                if (serviceController != null)
                    return serviceController;

                // Wait before cycling.
                Thread.Sleep(5000);
            } while (true);

        }

        public void UpdateMethod()
        {
            try
            {
                ServiceController _serviceController = WaitForServiceStartup(_serviceName, 300);
                if (_serviceController == null)
                {
                    MessageBox.Show(String.Format(CultureInfo.InvariantCulture, Resources.InvalidService, _serviceName),
                        Resources.MonitorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                    //do
                    //{
                    //    // Wait before cycling.
                    //    Thread.Sleep(1000);
                    //    // Block if the service is paused or is shutting down.
                    //    _pause.WaitOne();
                    //} while (true);
                }

                ServiceControllerStatus Status = ServiceControllerStatus.PausePending;

                int wakeupInterval = 1000;
                DateTime wakeupTime = DateTime.Now.AddMilliseconds(wakeupInterval);

                do
                {
                    if (DateTime.Now > wakeupTime)
                    {
                        _serviceController.Refresh();
                        if (Status != _serviceController.Status)
                        {
                            Status = _serviceController.Status;
                            if (Status == ServiceControllerStatus.Stopped)
                                _notifyIcon.Icon = Resources.servicestopped;
                            else if (Status == ServiceControllerStatus.Running)
                                _notifyIcon.Icon = Resources.servicerunning;
                            else
                                _notifyIcon.Icon = Resources.servicepaused;
                        }

                        wakeupTime = DateTime.Now.AddMilliseconds(wakeupInterval);
                    }

                    // Wait before cycling.
                    Thread.Sleep(300);
                    // Block if the service is paused or is shutting down.
                    _pause.WaitOne();
                } while (true);
            }
            catch (InvalidOperationException iox)
            {
                MessageBox.Show(String.Format(CultureInfo.InvariantCulture, Resources.NotifyIconException, _serviceName, iox.Message), 
                    Resources.MonitorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
            }
            catch (Win32Exception ex)
            {
                MessageBox.Show(String.Format(CultureInfo.InvariantCulture, Resources.Win32Exception, ex.Message),
                    Resources.MonitorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format(CultureInfo.InvariantCulture,Resources.NotifyIconException, _serviceName, ex.Message),
                    Resources.MonitorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }


}
